package com.xzy.controller;


import com.xzy.config.Audience;
import com.xzy.entity.Department;
import com.xzy.entity.Report;
import com.xzy.entity.User;
import com.xzy.entity.mReport;
import com.xzy.service.DepartmentService;
import com.xzy.service.ReportService;
import com.xzy.service.UserService;
import com.xzy.service.mReportService;
import com.xzy.util.DateUtils;
import com.xzy.util.JSONResult;
import com.xzy.util.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private Audience audience;
    @Autowired
    private ReportService reportService;
    @Autowired
    private mReportService mreportService;
//登录
    @PostMapping("/zreport/user/login")
    public JSONResult login(@RequestParam("userName") String userName, @RequestParam("userPass") String userPass, HttpServletResponse response)
    {
        User user=userService.login(userName,userPass);
        if(user==null)
        {
            return JSONResult.ERROR("没有此用户！");
        }
        else
        {
            Department department=departmentService.getDepartment(user.getDepartmentId());
            String departmentName=department.getDepartmentName();
            user.setDepartmentName(departmentName);
            String userId=Integer.toString(user.getUserId());
            String token= JWTUtils.createJWT(userId,userName,audience);
            response.setHeader(JWTUtils.AUTH_HEADER_KEY,JWTUtils.TOKEN_PREFIX+token);
            Map<String,Object> map=new HashMap<String,Object>();
            map.put("token",token);
            map.put("user",user);
            return JSONResult.SUCCESS(map);

        }
    }
//显示所有上报人
    @GetMapping("/zreport/user/getAllUser")
    public JSONResult getAllUser(HttpServletRequest request)
    {
         String token = request.getHeader("Authorization");
         String _userId=JWTUtils.getUserId(token,audience.getBase64Secret());
         int userId=Integer.parseInt(_userId);
         User user=userService.getUser(userId);
         boolean userRights=user.getUserRights();
         if(!userRights)
         {
             return JSONResult.ERROR("您没有此权限！");
         }
         else
         {
             List<User> userList=userService.getAllUser();
             for(User u:userList)
             {
                 Department department=departmentService.getDepartment(u.getDepartmentId());
                 u.setDepartmentName(department.getDepartmentName());
             }
             return JSONResult.SUCCESS(userList);
         }
    }
//更新用户信息
    @PutMapping("/zreport/user/updateUser/{userId}")
    public JSONResult updateUser(User user,@PathVariable("userId")int userId,HttpServletRequest request)
    {
        String token = request.getHeader("Authorization");
        String _userId=JWTUtils.getUserId(token,audience.getBase64Secret());
        int luserId=Integer.parseInt(_userId);
        User luser=userService.getUser(luserId);
        boolean luserRights=luser.getUserRights();
        if(!luserRights)
        {
            return JSONResult.ERROR("您没有此权限！");
        }
        else
        {
            user.setUserId(userId);
            userService.updateUser(user);
            Department department=departmentService.getDepartmentByName(user.getDepartmentName());
            user.setDepartmentId(department.getDepartmentId());
            return JSONResult.SUCCESS(user);
        }
    }
//删除用户
    @DeleteMapping("/zreport/user/deleteUser/{userId}")
    public JSONResult deleteUser(@PathVariable("userId")int userId,HttpServletRequest request)
    {
        String token = request.getHeader("Authorization");
        String _userId=JWTUtils.getUserId(token,audience.getBase64Secret());
        int luserId=Integer.parseInt(_userId);
        User luser=userService.getUser(luserId);
        boolean luserRights=luser.getUserRights();
        if(!luserRights)
        {
            return JSONResult.ERROR("您没有此权限！");
        }
        else
        {
            userService.deleteUser(userId);
            return JSONResult.SUCCESS();
        }
    }
//添加用户
    @PostMapping("/zreport/user/addUser")
    public JSONResult addUser(User user,HttpServletRequest request)
    {
        String token = request.getHeader("Authorization");
        String _userId=JWTUtils.getUserId(token,audience.getBase64Secret());
        int luserId=Integer.parseInt(_userId);
        User luser=userService.getUser(luserId);
        boolean luserRights=luser.getUserRights();
        if(!luserRights)
        {
            return JSONResult.ERROR("您没有此权限！");
        }
        else
        {

          Department department=departmentService.getDepartmentByName(user.getDepartmentName());
          int departmentId=department.getDepartmentId();
          user.setDepartmentId(departmentId);
          userService.addUser(user);
          return JSONResult.SUCCESS(user);
        }
    }

    @PostMapping("/zreport/user/getUserByToken")
    public JSONResult getUserByToken(@RequestParam("token") String token)
    {
        String _userId=JWTUtils.getUserId(token,audience.getBase64Secret());
        int userId=Integer.parseInt(_userId);
        User user=userService.getUser(userId);
        Department department=departmentService.getDepartment(user.getDepartmentId());
        user.setDepartmentName(department.getDepartmentName());
        return  JSONResult.SUCCESS(user);
    }
   @GetMapping("/zreport/user/getUser")
    public JSONResult getUser(HttpServletRequest request) throws Exception {
       String token=request.getHeader("Authorization");
       String _userId=JWTUtils.getUserId(token,audience.getBase64Secret());
       int userId=Integer.parseInt(_userId);
       User user=userService.getUser(userId);
       Department department=departmentService.getDepartment(user.getDepartmentId());
       user.setDepartmentName(department.getDepartmentName());
       int week= DateUtils.getWeek();
       int lweek=week-1;
       int month=DateUtils.getMonth();
       int lmonth=month-1;
       String reportDate=DateUtils.getYear()+"年"+month+"月"+"第"+week+"周";
       String lreportDate=DateUtils.getYear()+"年"+month+"月"+"第"+lweek+"周";
       if(week==1)
       {
        lreportDate=DateUtils.getYear()+"年"+lmonth+"月"+"第"+4+"周";
       }
       Map<String,Object>map=new HashMap<>();
       Report report=reportService.getReportByDepartmentAndDate(user.getDepartmentId(),reportDate);
       Report report1=reportService.getReportByDepartmentAndDate(user.getDepartmentId(),lreportDate);
       if(report!=null)
       {
           map.put("reportstatus",true);
       }
       else
       {
           map.put("reportstatus",false);
       }
       if(report1!=null)
       {
           map.put("lreportstatus",true);
       }
       else
       {
           map.put("lreportstatus",false);
       }
       mReport mreport=mreportService.getmReportByDepartmentAndDate(user.getDepartmentId(),month);
       mReport mreport1=mreportService.getmReportByDepartmentAndDate(user.getDepartmentId(),lmonth);
       if(mreport!=null)
       {
           map.put("mreportstatus",true);
       }
       else
       {
           map.put("mreportstatus",false);
       }
       if(mreport1!=null)
       {
           map.put("lmreportstatus",true);
       }
       else
       {
           map.put("lmreportstatus",false);
       }
       map.put("user",user);
       return JSONResult.SUCCESS(map);
   }
   @GetMapping("/zreport/admin/getInfo")
   public JSONResult getInformation(HttpServletRequest request) throws Exception {
       String token=request.getHeader("Authorization");
       String _userId=JWTUtils.getUserId(token,audience.getBase64Secret());
       int userId=Integer.parseInt(_userId);
       User user=userService.getUser(userId);
       if(!user.getUserRights())
       {
           return JSONResult.PERMISSION_UNAUTHORISE();
       }
       int week= DateUtils.getWeek();
       int lweek=week-1;
       int month=DateUtils.getMonth();
       int lmonth=month-1;
       String reportDate=DateUtils.getYear()+"年"+month+"月"+"第"+week+"周";
       String lreportDate=DateUtils.getYear()+"年"+month+"月"+"第"+lweek+"周";
       if(week==1)
       {
           lreportDate=DateUtils.getYear()+"年"+lmonth+"月"+"第"+4+"周";
       }
       List<Integer> rlist=reportService.getDepartmetByDate(reportDate);
       List<Department> rdepartments=new ArrayList<>();
       List<Integer> lrlist=reportService.getDepartmetByDate(lreportDate);
       List<Department>lrdepartments=new ArrayList<>();
      for(int i=0;i<rlist.size();i++)
      {
         rdepartments.add(departmentService.getDepartment(rlist.get(i)));
      }
       for(int i=0;i<lrlist.size();i++)
       {
           lrdepartments.add(departmentService.getDepartment(lrlist.get(i)));
       }
       List<Integer> mlist=mreportService.getDepartments(month);
       List<Department> mdepartments=new ArrayList<>();
       List<Integer> lmlist=mreportService.getDepartments(lmonth);
       List<Department>lmdepartments=new ArrayList<>();
       for(int i=0;i<mlist.size();i++)
       {
           mdepartments.add(departmentService.getDepartment(mlist.get(i)));
       }
       for(int i=0;i<lmlist.size();i++)
       {
           lmdepartments.add(departmentService.getDepartment(lmlist.get(i)));
       }
       Map<String,Object> map=new HashMap<>();
       map.put("reportdepartment",rdepartments);
       map.put("lreportdepartment",lrdepartments);
       map.put("mreportdepartment",mdepartments);
       map.put("lmreportdepartment",lmdepartments);

       return JSONResult.SUCCESS(map);
   }




}
